<style scoped>
</style>
<!-- 编辑页面 SEO 设置的弹窗 -->
<template>
  <div class="host-robots-dialog" v-loading="loading">
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="960px" @click="close">
      <p> 通过访问 /sitemap.xml 查看编辑的内容
      </p>
      <div>
        <ACEDefault ref="aceCoding" mode="xml" :minLines="30" :maxLines="50" />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" size="small" @click="clear()">清 空</el-button>
        <el-button type="primary" size="small" @click="onSubmit()">保 存</el-button>
        <el-button type="success" size="small" @click="onSubmitAndClose()">保存并关闭</el-button>
        <el-button @click="dialogFormVisible = false" size="small">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ACEDefault from '@/components/common/ace/Default.vue'
export default {
  data () {
    return {
      title: '',
      dialogFormVisible: false,
      loading: false
    }
  },
  methods: {
    show () {
      // 初始化数据
      this.dialogFormVisible = true
      // 请求后台获取数据
      this.$nextTick(() => {
        this.$refs['aceCoding'].created()
        this.$refs['aceCoding'].setValue('')
      })
      this.loading = true
      this.$post('/host/get-hconfig-info', null).then(response => {
        if (response && response.id && parseInt(response.id)) {
          this.$refs['aceCoding'].setValue(response.sitemapxml ? response.sitemapxml : '')
        } else {
          this.$refs['aceCoding'].setValue('')
        }
        this.$refs['aceCoding'].editor().moveCursorTo(0, 0)
        this.$refs['aceCoding'].loaded()
        this.loading = false
      })
    },
    clear () {
      this.$refs['aceCoding'].setValue('')
      this.$refs['aceCoding'].editor().moveCursorTo(0, 0)
    },
    close () { },
    onSubmit (callback) {
      let sitemapxml = this.$refs['aceCoding'].getValue()
      if (sitemapxml.length > 102400) {
        this.$message.error('sitemap 内容不能大于 102400 个字符！')
        return
      }

      // 提交表单
      this.loading = true
      this.$post('/seo/save-hconfig-sitemapxml', {
        sitemapxml: sitemapxml
      }).then(response => {
        if (typeof callback === 'function') {
          callback()
        }
        this.loading = false
      })
    },
    onSubmitAndClose () {
      this.onSubmit(() => {
        this.dialogFormVisible = false
      })
    }
  },
  components: {
    ACEDefault
  }
}

</script>
